<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div hidden>
    <p id="setChannelkou">{:U('config/setChannelkou')}</p>
</div>
<div class="dropdown">

    <select id="id_channel">
        <foreach name="data" item="vo">
            <option value="{$vo['key']}">{$vo['key']}</option>
        </foreach>
    </select>
    <!--<div>-->
    <!--<foreach name="vo['select']" item="ve">-->
    <!--{$ve['index']}<input name="{$vo['key']}" type="checkbox" onclick="onclock('{$vo['key']}')"  id="aa" value="{$ve['index']}" {$ve[ 'selected']? "checked=''": ""}/>-->
    <!--&nbsp;-->
    <!--</foreach>-->
    <!--</div>-->

    <div id="privences"></div>

</div>

<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script>
    var privence = [
        "上海",
        "云南",
        "内蒙",
        "北京",
        "吉林",
        "四川",
        "天津",
        "宁夏",
        "安徽",
        "山东",
        "山西",
        "广东",
        "广西",
        "新疆",
        "江苏",
        "江西",
        "河北",
        "河南",
        "浙江",
        "海南",
        "湖北",
        "湖南",
        "甘肃",
        "福建",
        "西藏",
        "贵州",
        "辽宁",
        "重庆",
        "陕西",
        "青海",
        "黑龙江"
    ];

    function showPrivence(channel) {
        var ch = "<form id='channel_form' action='setChannelkou'> " +
            "<input type='text' name='channel' disabled value='" + channel + "' >" +
            "<input type='text' name='kou' value='1' placeholder='渠道扣率' >" +
            "<input id='channel_setting' type='submit'  value='设置'>" +

            "</form> <hr/>"
        var html = "<table class=\"table table-bordered table-hover definewidth m10\">\n" +
            "      \n" +
            "        <thead>\n" +
            "            <tr>\n" +
            "                <th>省份</th>\n" +
            "                <th>扣率</th>\n" +
            " <th>设置</th>\n" +
            "            </tr>\n" +
            "        </thead>\n" +
            "        <tbody>\n";
        privence.forEach(function (item, index, arr) {
            html += "<form id='" + item + "' ><tr><th width='10%'><input  type='text' disabled value='" + item + "'" + "/></th>" +
                "<th width='90%'><input type='text'  name='kou'></th>" +
                "<th ><input type='hidden'  name='channel' value='" + channel + "'></th>" +

                "<th width='90%'><input type='submit' onclick='return setPrivencekou("+index+")'></th></tr></form>"

        });


        var end = "        </tbody>\n" +
            "    </table>";
        html += end;
        ch += html;
        $("#privences").children().remove();
        $("#privences").append(ch);

        $("#channel_setting").click(function () {

            var targetUrl = $("#setChannelkou").text();
            var data = $("#channel_form").serialize();
            console.log(data);
            $.ajax({
                type: 'post',
                url: targetUrl,
                cache: false,
                data: data,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    alert('success' + targetUrl);
                },
                error: function () {
                    alert("请求失败")
                }
            })
            return false;

        })


    }

    $("#id_channel").change(function (obj) {
        channel = $(this).val();
        showPrivence(channel);

    })


    function setPrivencekou(index) {
        console.log(index);
        var targetUrl = $("#setChannelkou").text();
        var data = $("#"+privence[index]).serialize();
        console.log("param:"+data+privence[index]);
        $.ajax({
            type: 'post',
            url: targetUrl,
            cache: false,
            data: data,
            dataType: 'json',
            success: function (data) {
                console.log(data);
                alert('success' + targetUrl);
            },
            error: function () {
                alert("请求失败")
            }
        })
        return false;

    }
</script>
</body>
</html>